/*###################################################################*
#
# MP (Mitchell Pell) Base - CrossPlatform Base Utilities.
# Copyright (C) 2014  Mitchell Pell
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
####################################################################*/
#ifndef GL_TCP_SERVER_H
#define GL_TCP_SERVER_H

#include "TcpSession.h"
using namespace boost::asio::ip;
using mp_base::interfaces::RecursiveLockingMutex;
using mp_base::utility::Pause;
using mpgl_engine::driver::gl_console::GlConsole;

namespace mpgl_engine{ namespace application{ namespace net {

	/*!----------------------------------------------------------------
	*	@class		TcpServer
	*	@brief		This class represents a TCP Server, inheriting from
	*				the TCP Session base class. The Server variant is
	*				allowed multiple connections and has the ability
	*				to listen for incoming connections.
	*
	*	@author		Mitchell Pell
	*	@date		01/04/2014
	*	@version	1.0
	*
	*	@revisions	Revision History:
	*
	*					- Version 1.0 01/04/2014
	*						-# Initial Make
	*
	-----------------------------------------------------------------*/
	class TcpServer : public TcpSession
	{
	public:
		/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		>>> Constructor(s) & Destructor
		>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/

		/*! Default constructor. */
		TcpServer(GlConsole* console = NULL,
			io_service* ioService = NULL,
			uint maxConnections = NetworkDefines::SERVER_MAX_CLIENTS);
		/*! Destructor. */
		virtual ~TcpServer(){ ClearAcceptor(); }

		/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		>>> General Operation
		>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/

		/*!  */
		virtual void Initialize(){ SetAcceptor(); }
		/*! Updates server and connections. */
		virtual void Update(){}
		/*! Disconnects connections then server. */
		virtual void Disconnect(){}
		/*! Not used in server.
		* @note		May rename connect to represent both
		*			start accept in a server, and connect
		*			in a client.
		*/
		virtual void Connect(){ throw ""; }

		/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		>>> Client Accepting Methods
		>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/

		/*! Starts accepting a new client. */
		void StartAccept();
		/*! Results of trying to accept a new client. */
		virtual void HandleAccept(TcpConnection* newConnection,
			const boost::system::error_code& error);

		/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		>>> Client Disconnecting Methods
		>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/

		//TODO

		/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		>>> Accessors & Mutators
		>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/

		/*! 
		* @brief	Sets the acceptor to the current IO service listening on
		*			the given port.
		* @note		Currently operates with fixed port set in NetworkDefines.
		*/
		void SetAcceptor();
		/*!  */
		void ClearAcceptor(){ if (m_acceptor != NULL){
				delete m_acceptor; m_acceptor = NULL;}}

		/*>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
		>>> Members
		>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>*/
	protected:

		/*! Acceptor for accepting new clients. */
		tcp::acceptor* m_acceptor;
	};
}}}

#endif